<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_depot=new depot();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT"){
		if(isset($_POST["code_depot"])){$_depot->set_code_depot($_POST["code_depot"]);}
		if(isset($_POST["code_unite"])){$_depot->set_code_unite($_POST["code_unite"]);}
		if(isset($_POST["date_depot"])){$_depot->set_date_depot($_POST["date_depot"]);}
		if(isset($_POST["observation"])){$_depot->set_observation($_POST["observation"]);}
		$res=$_depot->INSERT(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE"){
		if(isset($_POST["code_depot"])){
		$_depot->set_code_depot($_POST["code_depot"]);
		$res=$_depot->DELETE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}
elseif($_POST["operation"]=="UPDATE"){
		if(isset($_POST["code_depot"])){$_depot->set_code_depot($_POST["code_depot"]);}
		if(isset($_POST["code_unite"])){$_depot->set_code_unite($_POST["code_unite"]);}
		if(isset($_POST["date_depot"])){$_depot->set_date_depot($_POST["date_depot"]);}
		if(isset($_POST["observation"])){$_depot->set_observation($_POST["observation"]);}
		$res=$_depot->UPDATE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["code_depot"]) && !empty($_POST["code_depot"])){
		$constraint.=" AND code_depot='".$_POST["code_depot"]."'";
	}

	if(isset($_POST["code_unite"]) && !empty($_POST["code_unite"])){
		$constraint.=" AND code_unite='".$_POST["code_unite"]."'";
	}

	if(isset($_POST["date_depot"]) && !empty($_POST["date_depot"])){
		$constraint.=" AND date_depot='".$_POST["date_depot"]."'";
	}

	if(isset($_POST["observation"]) && !empty($_POST["observation"])){
		$constraint.=" AND observation='".$_POST["observation"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_depot->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_depot->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>